import 'dart:async';

import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:untitled2/pages/scanner/barcode_scanner_simple.dart';

class MobileScanner extends StatefulWidget {
  const MobileScanner({Key? key}) : super(key: key);

  @override
  _MobileScannerState createState() => _MobileScannerState();
}

class _MobileScannerState extends State<MobileScanner> with WidgetsBindingObserver {
  MobileScannerController controller = MobileScannerController();
  StreamSubscription<Object?>? _subscription;

  void _handleBarcode(Object? barcode) {
    final String code = barcode.toString();
    print("barcode: $code");
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (!controller.value.hasCameraPermission) {
      return;
    }

    switch (state) {
      case AppLifecycleState.detached:
      case AppLifecycleState.hidden:
      case AppLifecycleState.paused:
        return;
      case AppLifecycleState.resumed:
        _subscription = controller.barcodes.listen(_handleBarcode);

        unawaited(controller.start());
      case AppLifecycleState.inactive:
        unawaited(_subscription?.cancel());
        _subscription = null;
        unawaited(controller.stop());
    }
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    // Start listening to the barcode events.
    _subscription = controller.barcodes.listen(_handleBarcode);
    unawaited(controller.start());
  }

  Widget _buildItem(BuildContext context, String label, Widget page) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.of(context).push(
              MaterialPageRoute(
                builder: (context) => page,
              ),
            );
          },
          child: Text(label),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("扫一扫"),
      ),
      body: Container(
        child: ListView(
          children: [
            _buildItem(
              context,
              'MobileScanner Simple',
              const BarcodeScannerSimple(),
            ),
            // _buildItem(
            //   context,
            //   'MobileScanner with ListView',
            //   const BarcodeScannerListView(),
            // ),
            // _buildItem(
            //   context,
            //   'MobileScanner with Controller',
            //   const BarcodeScannerWithController(),
            // ),
            // _buildItem(
            //   context,
            //   'MobileScanner with ScanWindow',
            //   const BarcodeScannerWithScanWindow(),
            // ),
            // _buildItem(
            //   context,
            //   'MobileScanner with Controller (return image)',
            //   const BarcodeScannerReturningImage(),
            // ),
            // _buildItem(
            //   context,
            //   'MobileScanner with zoom slider',
            //   const BarcodeScannerWithZoom(),
            // ),
            // _buildItem(
            //   context,
            //   'MobileScanner with PageView',
            //   const BarcodeScannerPageView(),
            // ),
            // _buildItem(
            //   context,
            //   'MobileScanner with Overlay',
            //   const BarcodeScannerWithOverlay(),
            // ),
            // _buildItem(
            //   context,
            //   'Analyze image from file',
            //   const BarcodeScannerAnalyzeImage(),
            // ),
          ],
        ),
      ),
    );
  }

  @override
  Future<void> dispose() async {
    // Stop listening to lifecycle changes.
    WidgetsBinding.instance.removeObserver(this);
    // Stop listening to the barcode events.
    unawaited(_subscription?.cancel());
    _subscription = null;
    // Dispose the widget itself.
    super.dispose();
    // Finally, dispose of the controller.
    await controller.dispose();
  }
}
